package com.bw.controller;

import cn.hutool.core.util.ObjectUtil;
import com.bw.domain.Cd;
import com.bw.domain.Yp;
import com.bw.service.CdService;
import com.bw.service.YpService;
import com.bw.utils.common.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RequestMapping("goods")
@RestController
public class GoodsController {
    @Autowired
    YpService ypService;
@Autowired
    CdService cdService;
    @RequestMapping("list")
    public Result list(@RequestBody Yp yp, @RequestParam(defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,3);
        List<Yp> list = ypService.list(yp);
        return Result.success(new PageInfo<>(list));
    }
    @RequestMapping("upFile")
    public Result upFile(MultipartFile file) throws IOException {
        if(ObjectUtil.isNotNull(file)){
            String path="d:/pic/";
            String filename = file.getOriginalFilename();
            String newFileName = UUID.randomUUID() + filename.substring(filename.lastIndexOf("."));
            File file1 = new File(path, newFileName);
            file.transferTo(file1);
            return Result.success(newFileName);
        }
        return Result.fail("请选择文件");
    }
    @Cacheable(value = "brands")
    @RequestMapping("cdId")
    public Result cdId(){
        List<Cd> list = cdService.list();
        return Result.success(list);
    }
    @RequestMapping("addUpdate")
    public Result add(@RequestBody Yp yp){
        boolean b = ypService.saveOrUpdate(yp);
        return b?Result.success():Result.fail();
    }
}
